import Cookies from "js-cookie"

function getStatus() {
  return Cookies.get('sidebar-status')
}
function getExpand() {
  return Cookies.get('sidebar-expand')
}

function setStatus() {
  Cookies.set('sidebar-status', +state.sidebar.open)
}

function setExpand() {
  Cookies.set('sidebar-expand', +state.sidebar.expand)
}

const STATUS = getStatus()

const EXPAND = getExpand()

const state = {
  sidebar: {
    open: STATUS ? !!+STATUS : true,
    expand: EXPAND ? !!+EXPAND : false,
    withoutAnimation: false
  },
  login: {
    withoutAnimation: false
  },
  device: 'desktop',

}

const mutations = {
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    state.sidebar.open = false
    state.sidebar.withoutAnimation = withoutAnimation
    setStatus()
  },
  TOGGLE_SIDEBAR: state => {
    state.sidebar.open = !state.sidebar.open
    state.sidebar.withoutAnimation = false
    setStatus()
  },
  CLOSE_EXPAND: state => {
    state.sidebar.expand = false
    setExpand()
  },
  TOGGLE_EXPAND: state => {
    state.sidebar.expand = !state.sidebar.expand
    setExpand()
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  TOGGLE_LOGIN: (state, withoutAnimation) => {
    state.login.withoutAnimation = withoutAnimation
  }
}

const actions = {
  closeSideBar({commit}, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleSideBar({commit}) {
    commit('CLOSE_EXPAND')
    commit('TOGGLE_SIDEBAR')
  },
  closeExpand({commit}) {
    commit('CLOSE_EXPAND')
  },
  toggleExpand({commit}) {
    commit('TOGGLE_EXPAND')
  },
  toggleDevice({commit}, device) {
    commit('TOGGLE_DEVICE', device)
  },
  toggleLogin({commit}, { withoutAnimation }) {
    commit('TOGGLE_LOGIN', withoutAnimation)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}